package com.spring.component.easyexcel;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;

public class ExcelWriteTest {

  public static void main(String[] args) {
    // creatTestExcel();
    // creatDemoExcel();
    // creatDemoExcel1();
    createExcel();
    // addSheet();
  }

  /**
   * 创建单个sheet的excel
   */
  public static void creatDemoExcel1() {
    String fileName = "G:\\tmp\\Demo1.xlsx";
    EasyExcel.write(fileName, DemoData.class).sheet("Demo")
        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(creteData("点赞"));
  }

  /**
   * 创建多个sheet的excel
   */
  public static void createExcel() {
    String fileName = "G:\\tmp\\Demo1.xlsx";
    ExcelWriter writer = EasyExcel.write(fileName, DemoData.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(0, "点赞").build();
    WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "投币").build();
    writer.write(creteData("点赞"), writeSheet);
    writer.write(creteData("投币"), writeSheet1);
    writer.finish();
  }

  public static void addSheet() {
    String fileName = "G:\\tmp\\Demo1.xlsx";
    // ExcelWriter writer = EasyExcel.write(fileName, DemoData.class).build();
    ExcelWriter writer = EasyExcel.write().file(new File(fileName))
        .autoCloseStream(Boolean.FALSE)
        .head(DemoData.class)
        .build();
    WriteSheet writeSheet = EasyExcel.writerSheet(2, "收藏").build();
    writer.write(creteData("收藏"), writeSheet);
    writer.finish();
    // EasyExcelFactory.getWriter();
  }



  public static void creatDemoExcel() {
    String fileName = "G:\\tmp\\Demo.xlsx";
    List<List<String>> head = new ArrayList<>();
    head.add(Arrays.asList("字符串标题"));
    head.add(Arrays.asList("日期标题"));
    head.add(Arrays.asList("数字标题"));
    head.add(Arrays.asList("枚举类"));
    List<List> data = new ArrayList();
    for (int i = 0; i < 10; i++) {
      List list = new ArrayList();
      list.add("点赞" + i);
      list.add(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒")));
      list.add("666%");
      list.add("one");
      data.add(list);
    }
    EasyExcel.write(fileName).head(head).sheet("Demo")
        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(data);
  }

  public static void creatTestExcel() {
    String fileName = "G:\\tmp\\" + System.currentTimeMillis() + ".xlsx";
    EasyExcel.write(fileName).head(head()).sheet(1, "模板")
        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(dataList());
  }

  public static List<List<String>> head() {
    List<List<String>> list = new ArrayList<>();
    // 第一列表头
    List<String> head0 = new ArrayList<String>();
    head0.add("第一列");
    head0.add("第一列第二行");
    // 第二列表头
    List<String> head1 = new ArrayList<String>();
    head1.add("第一列");
    head1.add("第二列第二行");
    // 第三列
    List<String> head2 = new ArrayList<String>();
    head2.add("第二列");
    head2.add("第三列第二行");
    list.add(head0);
    list.add(head1);
    list.add(head2);
    return list;
  }

  public static List dataList() {
    List<List<Object>> list = new ArrayList<List<Object>>();
    for (int i = 0; i < 10; i++) {
      List<Object> data = new ArrayList<Object>();
      data.add("点赞+" + i);
      // date 将会安装 yyyy-MM-dd HH:mm:ss 格式化
      data.add(new Date());
      data.add(0.56);
      data.add(1111111);
      list.add(data);
    }
    return list;
  }

  private static List<DemoData> creteData(String name) {
    List<DemoData> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      int h = new Random().nextInt(6) + 1;
      double k = new Random().nextDouble();
      list.add(new DemoData(name + i, new Date(), k, WeekEnum.valueOf("WEEK_" + h), null));
    }
    return list;
  }


}
